package com.cmfi.service.impl.PlcImpl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.milo.opcua.sdk.client.OpcUaClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cmfi.dao.PlcDao.PlcDao;
import com.cmfi.domain.PlcEntity.Plc;
import com.cmfi.service.inter.PlcInter.PlcService;


@Service
public class PlcServiceImpl implements PlcService {
	//key是plc的connectAddress，value是plc连接
	private Map<String, OpcUaClient> opcClientMap = new ConcurrentHashMap<>();
	@Autowired
	private PlcDao plcDao;
	
	static Logger logger = LoggerFactory.getLogger(PlcServiceImpl.class);

	@Override
	public List<Plc> getAllPlc() {
		return plcDao.getAllPlc();
	}
	
	@Override
	public void addPlc(Plc plc) {
		 plcDao.savePlc(plc);
	}
	
	@Override
	public void delPlcByPlcNum(Long plcNum) {
		plcDao.deleteByPlcNum(plcNum);
	}

	public Map<String, OpcUaClient> getOpcClientMap() {
		return opcClientMap;
	}

	public void setOpcClientMap(Map<String, OpcUaClient> opcClientMap) {
		this.opcClientMap = opcClientMap;
	}

	@Override
	public OpcUaClient getOpcClientByConnectAddress(String connectAddress) {
		return opcClientMap.get(connectAddress);
	}
	
}
